package com.ymatou.iapi.productquery.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productquery.parameter.ProductList.ProductInListBean;
import com.ymatou.iapi.productquery.service.GetHotRecmdProductListBySellerIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

public class Ts_GetHotRecmdProductListBySellerId {
	private static GetHotRecmdProductListBySellerIdCall getHotRecmdProductListBySellerIdCall = new GetHotRecmdProductListBySellerIdCall();
	
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手热推商品列表");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getHotRecmdProductListBySellerIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	

	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetHotRecmdProductListBySellerId_001() {
		Logger.start(true,"买手热推商品列表");
		try{
			//1 remove all productInTop
			SellerProductCallService.removeAllProductInTop(sellerId);
			
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			SellerProductCallService.addProductsIntoTopList(sellerId, Arrays.asList(productId));
			
			
			checkProdBySellerId(sellerId,Arrays.asList(productId),0,0);
			
			//==================================================================================
			SellerProductCallService.removeAllProductInTop(sellerId);
			
			String productId1 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String productId2 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			SellerProductCallService.addProductsIntoTopList(sellerId, Arrays.asList(productId1,productId2));
			
			checkProdBySellerId(sellerId,Arrays.asList(productId1,productId2),0,0);
			
			
		}catch(Exception e){
			
		}
	}
	
	
	
	
	
	
	public static void checkProdBySellerId(int sellerId,List<String> prodList,int prodInActivityId,int liveId)throws Exception{
		YMTDateUtil.waitTime(2);
		List<ProductInListBean> tgtProductInListBeanList = new ArrayList<>();
		prodList.forEach(ele->{
			try {
				tgtProductInListBeanList.add(ProductQueryCallService.getProductInListBean(ele, prodInActivityId, liveId));
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		
		
		JSONObject ret = getHotRecmdProductListBySellerIdCall.search(Arrays.asList(((Integer)sellerId).toString()));
		String chkdesc = "GetHotRecmdProductListBySellerId";
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码检查");
		
		List<ProductInListBean> actProductInListBeanList = new ArrayList<>();
		JSONArray prodListAry = ret.getJSONObject("Data").getJSONArray("ProductList");
		for(int i=0;i<prodListAry.length();i++){
			actProductInListBeanList.add(ProductQueryCallService.getBeanFromJson(prodListAry.getJSONObject(i),ProductInListBean.class));
		}
		
		
		ProductQueryCallService.compareProductInListBean(prodList,tgtProductInListBeanList, actProductInListBeanList,chkdesc);
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
